package com.api.gbhg.luoao.tool;

import weaver.conn.RecordSet;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;

public class ToolCURD {

    /**
     * requestInfo根据主表id获取单个明细表数据
     *
     */
    public static RecordSet selectDetailnanlist(RequestInfo requestInfo,int subscript){
        //获取明细表数据
        DetailTable[] detailTables = requestInfo.getDetailTableInfo().getDetailTable();
        String tableDBName = detailTables[subscript].getTableDBName();//明细表名
        RecordSet rs  = new RecordSet();
        int billid = requestInfo.getRequestManager().getBillid();
        rs.executeQuery("select * from  "+tableDBName+"  where mainid = ?",billid);
        return rs;
    }




    /**
     * 根据主表id查询数据
     */
    public static RecordSet selectMastermeterByid(RequestInfo requestInfo){
        int billid = requestInfo.getRequestManager().getBillid();
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        if(billid != 0 && billTableName != null){
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from "+billTableName+" where id = ?",billid);
            return rs;
        }

        return null;
    }


    /**
     * 插入数据
     */
    public static boolean updateVersionQuery(double je,String requestid){
        RecordSet recordSet = new RecordSet();
        boolean b = recordSet.executeUpdate("UPDATE  uf_jingfei_version  SET yszje = ? WHERE requestId = ?",je, requestid);
        return b;

    }

    public static String Processnode(String requestId){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select n.nodename from workflow_requestbase r\n" +
                " left join workflow_nodebase  n on r.currentnodeid=n.id\n" +
                " where r.requestid ="+requestId);
        rs.next();
        return  rs.getStringNoTrim("nodename");
    }

    /**
     * 提供精确减法运算的sub方法
     * @param value1 被减数
     * @param value2 减数
     * @return 两个参数的差
     */
    public static BigDecimal sub(BigDecimal value1,BigDecimal value2){

        return value1.subtract(value2).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
